package com.burnerpat.fl2d.math;

public class FastMath
{
	public static final int PRECISION = 1000;
	public static final int INTPI = (int) Math.PI * PRECISION;

	private static double[] sin = new double[INTPI * 2];
	private static double[] cos = new double[INTPI * 2];

	public static final double PI = Math.PI;
	public static final double PIHALF = Math.PI / 2.0;
	public static final double PIQUART = Math.PI / 4.0;

	private static FastMath instance = new FastMath();

	private FastMath()
	{
		if (instance != null)
		{
			return;
		}

		for (int i = 0; i < sin.length; i++)
		{
			double p = (double) i / (double) PRECISION;

			sin[i] = Math.sin(p);
			cos[i] = Math.cos(p);
		}
	}

	public static FastMath getInstance()
	{
		return instance;
	}

	public static double sin(double x)
	{
		int i = (int) (x * (double) PRECISION) % sin.length;

		return sin[(i > 0) ? i : (sin.length + i)];
	}

	public static double cos(double x)
	{
		int i = (int) (x * (double) PRECISION) % cos.length;

		return cos[(i > 0) ? i : (cos.length + i)];
	}

	public static double atan(double x)
	{
		return Math.atan(x);
	}

	public static double abs(double x)
	{
		return (x > 0) ? x : -x;
	}

	public static double sqr(double x)
	{
		return x * x;
	}

	public static double sqrt(double x)
	{
		return Math.sqrt(x);
	}
}
